package com.pay.controller.admin;

import com.pay.model.auto.PayOrderExample;
import com.pay.model.auto.TsysUser;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.github.pagehelper.PageInfo;
import com.pay.common.base.BaseController;
import com.pay.common.domain.AjaxResult;
import com.pay.model.auto.PayOrder;
import com.pay.model.custom.TableSplitResult;
import com.pay.model.custom.Tablepar;
import com.pay.model.custom.TitleVo;
import com.pay.service.PayOrderService;
import io.swagger.annotations.Api;

import java.math.BigDecimal;
import java.util.List;

@Controller
@RequestMapping("PayOrderController")
@Api(value = "支付订单")
public class PayOrderController extends BaseController {

    private String prefix = "admin/payOrder";
    @Autowired
    private PayOrderService payOrderService;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @GetMapping("view")
    @RequiresPermissions("gen:payOrder:view")
    public String view(Model model) {
        String str = "支付订单";
        setTitle(model, new TitleVo("列表", str + "管理", true, "欢迎进入" + str + "页面", true, false));
        return prefix + "/list";
    }


    @PostMapping("list")
    @RequiresPermissions("gen:payOrder:list")
    @ResponseBody
    public Object list(Tablepar tablepar, BigDecimal searchTxt) {
        PageInfo<PayOrder> page = payOrderService.list(tablepar, searchTxt);
        TableSplitResult<PayOrder> result = new TableSplitResult<PayOrder>(page.getPageNum(), page.getTotal(), page.getList());
        return result;
    }

    /**
     * 新增
     */
    @GetMapping("/add")
    public String add(ModelMap modelMap) {
        return prefix + "/add";
    }


    @PostMapping("add")
    @RequiresPermissions("gen:payOrder:add")
    @ResponseBody
    public AjaxResult add(PayOrder payOrder, Model model) {
        int b = payOrderService.insertSelective(payOrder);
        if (b > 0) {
            return success();
        } else {
            return error();
        }
    }

    /**
     * 删除用户
     *
     * @param ids
     * @return
     */
    @PostMapping("remove")
    @RequiresPermissions("gen:payOrder:remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        int b = payOrderService.deleteByPrimaryKey(ids);
        if (b > 0) {
            return success();
        } else {
            return error();
        }
    }

    /**
     * 检查用户
     *
     * @param payOrder
     * @return
     */
    @PostMapping("checkNameUnique")
    @ResponseBody
    public int checkNameUnique(PayOrder payOrder) {
        int b = payOrderService.checkNameUnique(payOrder);
        if (b > 0) {
            return 1;
        } else {
            return 0;
        }
    }


    /**
     * 修改跳转
     *
     * @param id
     * @param mmap
     * @return
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap) {
        mmap.put("PayOrder", payOrderService.selectByPrimaryKey(id));

        return prefix + "/edit";
    }

    /**
     * 修改保存
     */
    @RequiresPermissions("gen:payOrder:edit")
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(PayOrder record) {
        return toAjax(payOrderService.updateByPrimaryKeySelective(record));
    }

    /**
     * 支付页面
     * @param id
     * @return
     */
    @PostMapping("/update")
    @ResponseBody
    public AjaxResult update(String id) {
        TsysUser tsysUser = (TsysUser) SecurityUtils.getSubject().getPrincipal();
        System.out.println(tsysUser);

        PayOrderExample example = new PayOrderExample();
        example.createCriteria().andIdEqualTo(id);
        List<PayOrder> list = payOrderService.selectByExample(example);
        if(null == list || list.size() < 1){
            return error("订单不存在");
        }
        // 修改订单
        PayOrder order = list.get(0);
        payOrderService.updateByPrimaryKeySelective(PayOrder.builder()
                .updateUser(tsysUser.getUsername())
                .payStatus(1)
                .id(order.getId())
                .build());

        String key = "pay_money_".concat(String.valueOf(order.getMoney()));
        stringRedisTemplate.delete(key);
        return success();
    }
}
